Imports TreeCreeper01
Imports TreeCreeper02
Imports TreeCreeper01Interfaces

Public Class fMain

    Private mCurrentStudy As IStudySpatial
    'Private mColouringList As cSpeciesColouringList
    Private mCurrentStudyName As String
    Private mDataAgentSpatial As IDataAgentSpatial

    Private Sub PopulateStudy(ByVal Filename As String, Optional ByVal TaxonomicOverrideFilename As String = "")

        Dim Study As IStudySpatial

        mDataAgentSpatial = TreeCreeper02.CreateSpatialDataAgent(Filename, True, TaxonomicOverrideFilename)

        Me.Cursor = Cursors.WaitCursor

        Study = New cStudySpatial

        mDataAgentSpatial.PopulateStudySpatial(Study)

        mCurrentStudyName = GetFileTitleFromFilename(Filename)
        mCurrentStudy = Study

        UpdateCaption()
        DisplayAllPlantDiversity()

        Me.ButtonDoAnalysis.Enabled = True
        Me.ButtonShowTaxonomicTree.Enabled = True
        Me.ButtonShowMap.Enabled = True

        Me.Cursor = Cursors.Default

    End Sub

    Private Sub DisplayAllPlantDiversity()

        Dim BaseSpeciesList As ISpeciesAbundanceList
        Dim OverallDiversity As Double

        With mCurrentStudy
            BaseSpeciesList = .CreateBaseSpeciesAbundanceList()
            OverallDiversity = BaseSpeciesList.SpeciesListAnalysis.CalculateGenericNonSpatialTaxonomicDiversity
            TextAllPlantsDiversity.Text = CStr(OverallDiversity)
        End With

    End Sub

    Private Sub ButtonSelectFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSelectFile.Click

        Dim MyOpenFileDialog As OpenFileDialog
        Dim Filename As String

        MyOpenFileDialog = New OpenFileDialog
        With MyOpenFileDialog
            .Title = "Select a CSV file with spatial data for analysis"
            .DefaultExt = "csv"
            .CheckFileExists = True
            .Filter = "Spatial files (*.csv)|*.csv"
            .ShowDialog()
            Filename = .FileName
        End With

        If Not Filename = "" Then
            TextFilename.Text = Filename
            ButtonCreateStudy.Enabled = True
        End If

    End Sub

    Private Sub UpdateCaption()

        Dim TheCaption As String

        TheCaption = " TreeCreeper Spatial " & Application.ProductVersion.ToString
        If mCurrentStudyName <> "" Then
            TheCaption = TheCaption & " - " & mCurrentStudyName
        End If
        Me.Text = TheCaption

    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        'Populate the combo box with recent files for this application

    End Sub

    Private Sub fMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        UpdateCaption()
        ButtonSelectFile_Click(sender, e)

    End Sub

    Private Function GetFileTitleFromFilename(ByVal Filename As String) As String

        Dim Index As Integer

        Index = InStrRev(Filename, "\")
        If Index > 0 Then
            Index = Len(Filename) - Index
            Return Microsoft.VisualBasic.Right(Filename, Index)
        Else
            Return Filename
        End If

    End Function

    Private Sub ButtonDoAnalysis_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDoAnalysis.Click

        Dim NewBatcher As fSpatialBatchAnalysis

        NewBatcher = New fSpatialBatchAnalysis
        NewBatcher.OfferBatch(mCurrentStudy, mCurrentStudyName, mDataAgentSpatial) ', mColouringList)

    End Sub

    Private Sub ButtonShowTaxonomicTree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonShowTaxonomicTree.Click

        Dim NewTreeDisplay As fSpeciesTree

        NewTreeDisplay = New fSpeciesTree
        NewTreeDisplay.DisplayTaxonomicTree(mCurrentStudy, "Taxonomic tree from: " & mCurrentStudyName)

    End Sub

    Private Sub ButtonExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonExit.Click

        Me.Close()

    End Sub

    Private Sub ButtonShowMap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonShowMap.Click

        Dim MapWindow As New fMap(mCurrentStudy.PlantList)

        MapWindow.Display("Actual arrangement")

    End Sub


    Private Sub CheckOverride_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckOverride.CheckedChanged

        Me.ButtonSelectOverrideFile.Enabled = CheckOverride.Checked
        Me.TextOverrideFilename.Enabled = CheckOverride.Checked

    End Sub

    Private Sub ButtonSelectOverrideFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSelectOverrideFile.Click

        Dim MyOpenFileDialog As OpenFileDialog
        Dim Filename As String

        MyOpenFileDialog = New OpenFileDialog
        With MyOpenFileDialog
            .Title = "Select an text file with the override data"
            .DefaultExt = "txt"
            .CheckFileExists = True
            .Filter = "Text files (*.txt)|*.txt"
            .ShowDialog()
            Filename = .FileName
        End With

        If Not Filename = "" Then
            TextOverrideFilename.Text = Filename
        End If

    End Sub

    Private Sub ButtonCreateStudy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCreateStudy.Click

        Dim Filename As String
        Dim OverrideFilename As String

        Filename = Me.TextFilename.Text
        If Me.CheckOverride.Checked Then
            OverrideFilename = Me.TextOverrideFilename.Text
        Else
            OverrideFilename = ""
        End If

        PopulateStudy(Filename, OverrideFilename)

        Me.Activate()

    End Sub

End Class
